#include <iostream>
#include "fft.hpp"

real_fft::real_fft(int size)
{
    mSize = size;
    mIn = (double*) fftw_malloc(sizeof(double)*mSize);
    mOut = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*(mSize/2+1));
    mPlan = fftw_plan_dft_r2c_1d(mSize, mIn, mOut, FFTW_ESTIMATE);
}

real_fft::~real_fft()
{
    fftw_destroy_plan(mPlan);
    fftw_free(mIn);
    fftw_free(mOut);
}

void real_fft::operator()(vector* in, cvector* out)
{
    if(in->size() != mSize) {
        std::cerr << "Wrong size of input vector" << std::endl;
        throw;
    }
    for(int i=0;i<mSize;i++) mIn[i] = in->get(i);
    fftw_execute(mPlan);
    if(out->size() != (mSize/2+1)) {
        out->resize(mSize/2+1);
    }
    for(int i=0;i<(mSize/2+1);i++) out->set(i,mOut[i][0],mOut[i][1]);
}